package com.hsav.kcxb.view;


import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.hsav.kcxb.R;

/**
 * Created by zhao on 2017/2/11.
 */

public class CustomDialog extends DialogFragment {

    private View mRootView;
    private String mContentStr;
    private OnClickHandler mOnClickHandler;
    private TextView mContentTx;
    private Button mCancelBtn;
    private Button mOkBtn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        mRootView = inflater.inflate(R.layout.dialog_set_layout,container,false);
        initView();
        return mRootView;
    }

    private void initView() {
        mContentTx = (TextView)mRootView.findViewById(R.id.tx_dialog_content);
        mContentTx.setText(mContentStr);

        mCancelBtn = (Button)mRootView.findViewById(R.id.btn_dialog_cancel);
        mCancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnClickHandler!=null){
                    mOnClickHandler.onCancelClick();
                }
                CustomDialog.this.dismissAllowingStateLoss();
            }
        });

        mOkBtn = (Button)mRootView.findViewById(R.id.btn_dialog_ok);
        mOkBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnClickHandler!=null){
                    mOnClickHandler.onOkClick();
                }
                CustomDialog.this.dismissAllowingStateLoss();
            }
        });
    }

    public void setContentStr(String contentStr){
        this.mContentStr = contentStr;
    }

    public void setOnClickListener(OnClickHandler onClickListener){
        this.mOnClickHandler = onClickListener;
    }

//    @NonNull
//    @Override
//    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
//        View view = layoutInflater.inflate(R.layout.dialog_set_layout,null);
//        builder.setView(view);
//        builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(getActivity(), "positive", Toast.LENGTH_SHORT).show();
//            }
//        }).setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(getActivity(),"cancle",Toast.LENGTH_LONG).show();
//            }
//        });
//        return builder.create();
//    }

    public interface OnClickHandler{
        void onCancelClick();
        void onOkClick();
    }
}
